import { _decorator, Component, Node } from 'cc';
import { CatCard } from './CatCard';
import { GameScene } from './GameScene';
import { Global } from '../Global';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
const { ccclass, property } = _decorator;

@ccclass('CatCardLoad')
/**
 * 猫咪卡牌层
 */
export class CatCardLoad extends Component {

    //猫咪卡牌数组
    @property([Node])
    public CatCardArray: Node[] = [];

    protected start(): void {
        this.Init();
    }

    public Init() {
        for (let _chl of this.node.children) {
            for (let _back of _chl.children) {
                _back.active = true;
            }
        }

        for (let cat of this.CatCardArray) {
            this.UpdateCat(cat);
        }
    }

    /**
     * 更新卡牌信息
     */
    public UpdateCat(_catcard: Node) {
        let ran_num: number = Math.floor(Math.random() * GameScene.Instance.CatCardInfo.length)
        _catcard.getComponent(CatCard).UpdateCard(GameScene.Instance.CatCardInfo[ran_num])
        GameScene.Instance.CatCardInfo.splice(ran_num, 1);
    }

    /**
     * 判断游戏结束
     */
    public CheckGameOver() {
        let is_over: boolean = true;

        for (let cat of this.CatCardArray) {
            if (cat.active) is_over = false;
        }

        if (is_over) {
            LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.SUCCESS++;
            LocalStorageMgr.Instance.UpdateLocalStroage();
            GameScene.Instance.GameOver();
        }
    }

}


